<template>
	<view>

		<view class="name">
			<image style="width: 220rpx;height: 130rpx;" :src="vuex_config.wap_login_logo" mode=""></image>
			<text style="margin-top: 30rpx;"> {{vuex_config.store_title}}</text>
		</view>
		<view class="btn-box">
			<!-- #ifdef MP -->
			<button type="primary" open-type="getPhoneNumber" @getphonenumber="onLoginByPhone">手机号快捷登录</button>
			<!-- #endif -->

			<!-- #ifdef H5 -->
			<button type="primary" @click="onLogin">快捷登录</button>
			<!-- #endif -->

		</view>
		<my-privacy></my-privacy>
	</view>
</template>

<script>
	import {
		getCode,
		isWeiXin
	} from '../../common/helper';
	export default {
		data() {
			return {
				code: '',
				reid: 0
			};
		},
		methods: {
			onLogin() {
				if (isWeiXin()) {
					const page = window.location.href.split('?')[0];
					location.href =
						`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.vuex_config.appid}&redirect_uri=${page}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
				} else {
					uni.showToast({
						title: '请在微信访问',
						icon: 'none'
					})
				}
			},
			async onLoginByPhone(e) {
				uni.showLoading({
					title: '登录中。。。',
					mask: true
				})
				const data = await this.$post('api/mobile-login', {
					...e.detail,
					mp_code: this.code,
					reid: this.reid
				})
				console.info({
					data
				})
				if (data.code === 200) {
					uni.hideLoading()
					this.$u.vuex('user_id', data.data.user_id)
					this.$u.vuex('user_token', data.data.token)
					setTimeout(() => {
						this.$u.route({
							url: "pages/index/index",
							type: "reLaunch"
						})
					}, 1000)
					uni.$u.toast(data.msg)
				} else {
					uni.$u.toast('登录失败')
				}
			}
		},
		async onLoad(option) {
			if (this.isLogin) {
				this.$u.route({
					url: "pages/index/index",
					type: "reLaunch"
				})
				return
			}


			// #ifdef MP
			this.code = await getCode()

			if (option.scene) {
				const [_, reid] = decodeURIComponent(option.scene).split(':')
				this.reid = reid
			}

			if (option.reid) {
				this.reid = option.reid
			}
			// #endif

			if (option.code) {
				this.code = option.code

				this.onLoginByPhone({
					detail: {}
				})
			}
		}
	}
</script>

<style lang="scss">
	.name {
		padding: 300rpx 30rpx 100rpx 30rpx;
		font-size: 30rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.btn-box {
		padding: 30rpx;
	}
</style>